package com.collection.treesets;

import java.util.SortedSet;
import java.util.TreeSet;

public class GetSubSetFromTreeSetExample {
	 public static void main(String[] args) {
		 
		    //create TreeSet object
		    TreeSet tSet = new TreeSet();
		   
		    //add elements to TreeSet
		    tSet.add("1");
		    tSet.add("3");
		    tSet.add("2");
		    tSet.add("5");
		    tSet.add("4");
		 
		   
		    /*
		      To get the sub Set from Java TreeSet use,
		      SortedSet subSet(int from, int to) method of TreeSet class.
		     
		      This method returns portion of the TreeSet whose elements range from
		      from (inclusive) to to(exclusive).
		     
		      Please note that, the SortedSet returned by this method is backed by
		      the original TreeSet. So any changes made to SortedSet will be
		      reflected back to original TreeSet.
		    */
		   
		    SortedSet sortedSet = tSet.subSet("2","5");
		   
		    System.out.println("SortedSet Contains : " + sortedSet);
		   
		   
		  }
}
